﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using Bestlife.ERP.DataAccess;

public partial class Web_Registration : System.Web.UI.Page
{
    String gp = "";
    SqlDataReader rdrmg;
    String Dis_id;
    int i;

    protected void Page_Load(object sender, EventArgs e)
    {
        #region Set the Distributor ID
        String con = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
        Bestlife.ERP.DataAccess.clsCon.connect(con);

        String Ssql = "SELECT TOP 1 [Dis_ID]FROM [BestlifeDB].[dbo].[Tbl_Distributor]ORDER BY CAST (Dis_ID AS FLOAT)DESC";
        SqlDataReader rdR;
        rdR = clsCon.ReadTable(Ssql);
        if (rdR.Read())
        {
            Dis_id = rdR["Dis_ID"].ToString();
        }
        i = int.Parse(Dis_id) + 1;
        TextBoxDisID.Text = i.ToString();
        rdR.Close();
        #endregion

        #region FOR DATA MIGRATION


        if ((Application["id"] != null) && (Application["path"] != null))
        {
            gp=Application["path"].ToString();

            if (gp.Contains("Test.aspx"))
            {

                String id=TextBoxDisID.Text = Application["id"].ToString();

                String connStr = ConfigurationManager.ConnectionStrings["bestlife_phpConnectionString"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);


                String sqlqry2 = "SELECT * FROM dbo.dealer WHERE dealercode='" + id + "'";
                rdrmg = Bestlife.ERP.DataAccess.clsCon.ReadTable(sqlqry2);


                while (rdrmg.Read())
                {

                    TextBoxSurName.Text = rdrmg["nameinfull"].ToString();
                    // ComboBoxTitle.Text ="";
                    TextBoxGivenName.Text = rdrmg["nameinfull"].ToString();
                    TextBoxChequeName.Text = rdrmg["chequename"].ToString();
                    TextBoxAdd1.Text=rdrmg["address1"].ToString();
                    TextBoxAdd2.Text=rdrmg["address2"].ToString();
                    TextBoxTownCity.Text=rdrmg["address2"].ToString();

                    //  TextBoxState.Text="";

                    // TextBoxState.Text="";

                    TextBoxZipCode.Text = rdrmg["postalzip"].ToString(); 
                    ComboBoxCountry.Items.Add(rdrmg["country"].ToString());
                    TextBoxHomePhone.Text=rdrmg["telhome1"].ToString();
                    TextBoxMobileNo.Text= rdrmg["telmob1"].ToString();
                    TextBoxEmail.Text=rdrmg["email"].ToString();
                    TextBoxValidIDNo.Text = rdrmg["idno"].ToString();
                    TextBoxNationality.Text = rdrmg["nationality"].ToString();
                    DatePickerDOB.Text=rdrmg["birthday"].ToString();
                    TextBoxOccupation.Text=rdrmg["occupation"].ToString();
                    TextBoxNameofBusiness.Text=rdrmg["occupation"].ToString();
                    TextBoxOfficeNo.Text = rdrmg["teloffice"].ToString();
                   
                    //NomTextBoxName.Text="";
                    //NomComboBoxRelationship.Text="";
                    //NomTextBoxAdd.Text="";
                  //  ComboBoxSecurityQuestion.Text="";
                    //TextBoxSA.Text="";
                    //TextBoxSW.Text="";
                    TextBoxQAPin.Text = rdrmg["reletionship"].ToString(); 
                    //TextBoxPass.Text = "";
                    //TextBoxURL.Text = "";
                    //ShipTextBoxContact.Text = "";
                    //ShipTextBoxAdd1.Text = "";
                    //ShipTextBoxAdd2.Text = "";
                    //ShipTextBoxTownCity.Text = "";
                    //ShipTextBoxState.Text = "";
                    //ShipTextBoxZipCode.Text = "";
                    //ShipComboBoxCountry.Text = "";
                    //ShipTextBoxPhone.Text = "";
                    //ShipTextBoxMobile.Text = "";
                    //ShipTextBoxEmail.Text = "";
                    //TextBoxUserName.Text = "";


                }

                rdrmg.Close();



                
                //Bestlife.ERP.DataAccess.clsCon.connect(connStr);

                //String sqlqry3 = "SELECT * FROM dealerdetail WHERE dealercode='" + id + "'";
                //SqlDataReader rdrmg3 = Bestlife.ERP.DataAccess.clsCon.ReadTable(sqlqry3);

                //while (rdrmg3.Read())
                //{
                //    BenTextBoxName.Text = rdrmg3["beneficiaryname"].ToString();
                //    BenComboBoxRelationship.SelectedValue = rdrmg3["reletionship"].ToString();
                //    BenTextBoxAdd.Text = "";

                //}


                //rdrmg.Close();
            }
        }

        #endregion


        //Set the your details image to the header
        
        Session["key"] = "~/img/Process/3.png";

        //Disable Team 3 and Team 4 Radio Buttons
       
        #region
        RadioButtonTeam3.Enabled = false;
        RadioButtonTeam4.Enabled = false;
        #endregion

        //Disable All the Shipping Information (Non Editable Region)
        #region disable all the shipping information
        ShipTextBoxContact.Enabled = false;
        ShipTextBoxAdd1.Enabled = false;
        ShipTextBoxAdd2.Enabled = false;
        ShipTextBoxTownCity.Enabled = false;
        ShipTextBoxState.Enabled = false;
        ShipTextBoxZipCode.Enabled = false;
        ShipComboBoxCountry.Enabled = false;
        ShipTextBoxPhone.Enabled = false;
        ShipTextBoxMobile.Enabled = false;
        ShipTextBoxEmail.Enabled = false;

        //Disable All the Validation Part of Shipping Information

        RequiredFieldValidatorContact.Enabled = false;
        RequiredFieldValidatorAdd1.Enabled = false;
        RequiredFieldValidatorAdd2.Enabled = false;
        RequiredFieldValidatorTownCity.Enabled = false;
        RequiredFieldValidatorZipCode.Enabled = false;
        RequiredFieldValidatorCountry.Enabled = false;
        RequiredFieldValidatorPhoneNo.Enabled = false;
        RequiredFieldValidatorMobileNo.Enabled = false;
        RequiredFieldValidatorEmail.Enabled = false;
        RegularExpressionValidatorEmailVal.Enabled = false;
        RegularExpressionValidatorMobileNoNumber.Enabled = false;
        RegularExpressionValidatorPhoneNumberOnly.Enabled = false;
        RegularExpressionValidatorZipNumber.Enabled = false;
        #endregion

    }


    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (!CheckBox1.Checked)
        {
            //Enable All the Shipment information after the check box is CHECKED !
            #region Enable all the shipment information
            ShipTextBoxContact.Enabled = true;
            ShipTextBoxAdd1.Enabled = true;
            ShipTextBoxAdd2.Enabled = true;
            ShipTextBoxTownCity.Enabled = true;
            ShipTextBoxState.Enabled = true;
            ShipTextBoxZipCode.Enabled = true;
            ShipComboBoxCountry.Enabled = true;
            ShipTextBoxPhone.Enabled = true;
            ShipTextBoxMobile.Enabled = true;
            ShipTextBoxEmail.Enabled = true;

            //Enable All the Validation Part of the Shipping Information

            //RequiredFieldValidatorContact.Enabled = true;
            //RequiredFieldValidatorAdd1.Enabled = true;
            //RequiredFieldValidatorAdd2.Enabled = true;
            //RequiredFieldValidatorTownCity.Enabled = true;
            //RequiredFieldValidatorZipCode.Enabled = true;
            //RequiredFieldValidatorCountry.Enabled = true;
            //RequiredFieldValidatorPhoneNo.Enabled = true;
            //RequiredFieldValidatorMobileNo.Enabled = true;
            //RequiredFieldValidatorEmail.Enabled = true;
            //RegularExpressionValidatorEmailVal.Enabled = true;
            //RegularExpressionValidatorMobileNoNumber.Enabled = true;
            //RegularExpressionValidatorPhoneNumberOnly.Enabled = true;
            //RegularExpressionValidatorZipNumber.Enabled = true;
            #endregion
        }
    }


    protected void ASPxButton2_Click(object sender, EventArgs e)
    {
        //Sending data to the E_Registration
        //save file in folder


        Bestlife.ERP.Entities.E_Registration dis=new Bestlife.ERP.Entities.E_Registration();
        try
        {
            #region Team Selection And APIN
            if (RadioButtonTeam1.Checked)
            {
                dis.Team1 = 1;
            }
            else if(RadioButtonTeam2.Checked)
            {
                dis.Team1 = 2;
            }
            else if (RadioButtonTeam3.Checked)
            {
                dis.Team1 = 3;
            }
            else if(RadioButtonTeam4.Checked)
            {
                dis.Team1 = 4;
            }

            dis.APIN1 = TextBoxAPIN.Text;
            #endregion


            #region Pass all the data to entities
            dis.Sup_ID1 = TextBoxSuperID.Text;
            dis.Dis_Id1 = TextBoxDisID.Text;
            dis.Title1 = ComboBoxTitle.Text;
            dis.Sur_Name1 = TextBoxSurName.Text;
            dis.Giv_Name1 = TextBoxGivenName.Text;
            dis.Che_Name1 = TextBoxChequeName.Text;
            dis.Address11 = TextBoxAdd1.Text;
            dis.Address21 = TextBoxAdd2.Text;
            dis.City1 = TextBoxTownCity.Text;
            dis.Province1 = TextBoxState.Text;
          //  dis.Zip_Code1 = int.Parse(TextBoxZipCode.Text);
            dis.Country1 = ComboBoxCountry.SelectedValue.ToString();
            dis.Home_Tel_No1 = TextBoxHomePhone.Text;
            dis.Mob_No1 = TextBoxMobileNo.Text;
            dis.Email1 = TextBoxEmail.Text;
            dis.Valid_ID_Type1 = ComboBoxValidIDType.SelectedValue.ToString();
            dis.Valid_Id1 = TextBoxValidIDNo.Text;
            dis.Nationality1 = TextBoxNationality.Text;
            dis.Dob1 = DatePickerDOB.Text;
            dis.Occupation1 = TextBoxOccupation.Text;
            dis.Business_NameAdd1 = TextBoxNameofBusiness.Text;
            dis.Office_Tel1 = TextBoxOfficeNo.Text;
            dis.Beneficiary1 = BenTextBoxName.Text;
            dis.Ben_ID_Type1 = ComboBoxBenValidIDType.SelectedValue.ToString();
            dis.Ben_ID_No1 = BenTextBoxValidIDNo.Text;
            dis.Ben_Rel1 = BenComboBoxRelationship.SelectedValue.ToString();
            dis.Ben_Add1 = BenTextBoxAdd1.Text;
            dis.Ben_Add21 = BenTextBoxAdd2.Text;
            dis.Ben_Town1 = BenTextBoxTown.Text;
            dis.Ben_State1 = BenTextBoxState.Text;
            dis.Ben_Zip_Code1 = int.Parse(BenTextBoxZipCode.Text);
            dis.Sec_Question1 = ComboBoxSecurityQuestion.Text;
            dis.Answer1 = TextBoxSA.Text;
            dis.Sec_Word1 = TextBoxSW.Text;
         //   dis.Qa_Pin1 = int.Parse(TextBoxQAPin.Text);
            dis.Pas_Word1 = TextBoxPass.Text;
            dis.Web_Url1 = TextBoxURL.Text;
            dis.Ship_Contact1 = ShipTextBoxContact.Text;
            dis.Ship_Address11 = ShipTextBoxAdd1.Text;
            dis.Ship_Address21 = ShipTextBoxAdd2.Text;
            dis.Ship_City1 = ShipTextBoxTownCity.Text;
            dis.Ship_Province1 = ShipTextBoxState.Text;
            dis.Ship_Zip_Code1 = ShipTextBoxZipCode.Text;
            dis.Ship_Country1 = ShipComboBoxCountry.SelectedValue.ToString();
            dis.Ship_Hom_Tel_No1 = ShipTextBoxPhone.Text;
            dis.Ship_Mob_No1 = ShipTextBoxMobile.Text;
            dis.Ship_Email1 = ShipTextBoxEmail.Text;
            dis.UserName1 = TextBoxUserName.Text;
            #endregion
            
            #region Bank Details
            dis.Bank_Name1 = BankTextBoxName.Text;
            dis.Bank_Code1 = BankTextBoxCode.Text;
            dis.Branch_Name1 = BankTextBoxBranch.Text;
            dis.Branch_Code1 = BankTextBoxBRCode.Text;
            dis.Account_No1 = BankTextBoxAccount.Text;
            #endregion
            

            #region Level Genarator
            //Bestlife.ERP.BusinessRules.B_Level_Genarator lg = new Bestlife.ERP.BusinessRules.B_Level_Genarator();
            //string rvalue =lg.returnlevel(TextBoxDisID.Text, TextBoxSuperID.Text);

            //string[] arr = rvalue.Split('/');
            //string Parent_To_User = arr[0].ToString();
            //string Child_Level = arr[1].ToString();

            //Bestlife.ERP.Entities.E_Level_Genarator lvl = new Bestlife.ERP.Entities.E_Level_Genarator();

            //lvl.User_id = TextBoxDisID.Text;
            //lvl.Parent_to_user = Parent_To_User;
            //lvl.Level = Child_Level;

            //Bestlife.ERP.DataAccess.D_Level_Insert.addLevels(lvl);
            #endregion
            
            //Sending Data to Registration
            Bestlife.ERP.DataAccess.D_Registration.addDistributor(dis);
            //Sending Data to Bonus Table
            Bestlife.ERP.DataAccess.D_Registration.addBonusDetails(dis);
            //Sending Data to Bank Table
            Bestlife.ERP.DataAccess.D_Bank.addBankDetails(dis);
            //Sending Data to Check Username And Password
            Bestlife.ERP.DataAccess.D_UserPassword.addUserPassword(dis);
            LabelMessage.Text = "Your Details are Successfully Inserted, Thank You !";
        }
        catch(Exception) 
        {
            // Response.Redirect("404.aspx");
            LabelMessage.ForeColor = System.Drawing.Color.Red;
            LabelMessage.Text = "Your Details Cannot be inserted !";
        }  

}
    protected void ASPxButton2_Click1(object sender, EventArgs e)
    {

    }

    #region  Check Super Distributor


    protected void TextBoxSuperID_TextChanged(object sender, EventArgs e)
    {
        Bestlife.ERP.Entities.E_Referer super = new Bestlife.ERP.Entities.E_Referer();
        super.Super_Distributor1 = TextBoxSuperID.Text;
        string bb = Bestlife.ERP.BusinessRules.B_Referer.refername(super);
        LabelSupDisName.Text = bb;
        if (bb.Equals("Sorry, this does not seem to be a valid Referrer ID."))
        {

            Register.Visible = false;
        }
        else
        {
            Register.Visible = true;
            Register.Enabled = true;
        }

    }
    #endregion


    #region Check Availability for the User Name
    
    protected void TextBoxUserName_TextChanged(object sender, EventArgs e)
    {
        Bestlife.ERP.Entities.E_Registration username = new Bestlife.ERP.Entities.E_Registration();
        string uname = TextBoxUserName.Text;
        
        Bestlife.ERP.BusinessRules.B_User_Availability ava = new Bestlife.ERP.BusinessRules.B_User_Availability();
        string result = ava.uname(uname);

        if (result.Equals("1"))
         {
            Availability.ForeColor = System.Drawing.Color.Red;
            Availability.Text = "User Name is Not Available";
            Register.Visible = false;
         }
        else
        {
            Availability.ForeColor = System.Drawing.Color.Green;
            Availability.Text = "User Name is Available";
            Register.Visible = true;
            Register.Enabled = true;
        }
        //UNDER CONSTRUCTION
        #region Set the position to the textbox in postback
        if (IsPostBack)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "hash", "location.hash = '#username';", true);
        }
        #endregion

    }
    #endregion
    protected void RadioButtonTeam1_CheckedChanged(object sender, EventArgs e)
    {
        

    }
}